package com.bnyy.medicalHousekeeper.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.SpanUtils;
import com.bnyy.gbp.customsView.ScaleImageView;
import com.bnyy.medicalHousekeeper.GlideHelper;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.adapter.MedicalHousekeeperGoodsAdapter;
import com.bnyy.medicalHousekeeper.base.BaseActivityImpl;
import com.bnyy.medicalHousekeeper.bean.Article;
import com.bnyy.medicalHousekeeper.bean.Goods;
import com.bnyy.medicalHousekeeper.bean.MedicalHousekeeper;
import com.bnyy.medicalHousekeeper.bean.MineInfo;
import com.bnyy.medicalHousekeeper.moudle.message.activity.ChatMessageActivity;
import com.bnyy.medicalHousekeeper.request.BaseObserverImpl;
import com.bnyy.medicalHousekeeper.request.RequestManager;
import com.bnyy.message.bean.ContactUser;
import com.kevin.photo_browse.PhotoBrowse;
import com.kevin.photo_browse.ShowType;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2023/2/16
 **/
public class MedicalHousekeeperHomePageActivity extends BaseActivityImpl {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_header)
    ScaleImageView ivHeader;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_check_qualifications)
    TextView tvCheckQualifications;
    @BindView(R.id.tv_serve_count)
    TextView tvServeCount;
    @BindView(R.id.ll_score)
    LinearLayout llScore;
    @BindView(R.id.ll_article)
    LinearLayout llArticles;
    @BindView(R.id.tv_check_all_articles)
    TextView tvCheckAllArticles;
    @BindView(R.id.ll_goods)
    LinearLayout llGoods;
    @BindView(R.id.tv_check_all_goods)
    TextView tvCheckAllGoods;
    @BindView(R.id.tv_chat)
    TextView tvChat;
    @BindView(R.id.ll_articles_list)
    LinearLayout llArticlesList;
    @BindView(R.id.recycler_view_goods)
    RecyclerView recyclerViewGoods;
    @BindView(R.id.tv_title)
    TextView tvTitle;

    private int userId;

    public static void show(Context context, int userId) {
        Intent intent = new Intent(context, MedicalHousekeeperHomePageActivity.class);
        intent.putExtra("userId", userId);
        context.startActivity(intent);
    }

    @Override
    public boolean unuseRefreshLayout() {
        return true;
    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_medical_housekeeper_home_page;
    }

    @Override
    public String getTitleStr() {
        return null;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setStatusBarColor(Color.parseColor("#EEFBF1"));
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        userId = getIntent().getIntExtra("userId", -1);

        HashMap<String, Object> params = new HashMap<>();
        params.put("id", userId);
        requestManager.request(requestManager.mJavaRetrofitService.getMineInfo(RequestManager.getJsonRequestBody(params)),
                new BaseObserverImpl<MineInfo>() {
                    @Override
                    public void onSuccess(MineInfo mineInfo) {
                        super.onSuccess(mineInfo);

                        GlideHelper.setCircleImage(mContext, mineInfo.getUser_image(), ivHeader, 2, Color.WHITE);
                        tvName.setText(mineInfo.getName());
                        tvServeCount.setText("已服务" + mineInfo.getService_person_count() + "人");
                        tvCheckQualifications.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                PhotoBrowse.with(mContext)
                                        .showType(ShowType.MULTIPLE_URL)
                                        .url(mineInfo.getCredentials())
                                        .show();
                            }
                        });

                        getArticles();

                        getGoods();
                    }
                });

//        requestManager.request(requestManager.mJavaRetrofitService.getHousekeeperHomePage(RequestManager.getJsonRequestBody(params)),
//                new BaseObserverImpl<MedicalHousekeeper>(mContext) {
//                    @Override
//                    public void onSuccess(MedicalHousekeeper medicalHousekeeper) {
//                        super.onSuccess(medicalHousekeeper);
//
//                        tvTitle.setText(medicalHousekeeper.getUsername());
//
//                        GlideHelper.setCircleImage(mContext, medicalHousekeeper.getUser_img(), ivHeader, 2, Color.WHITE, R.mipmap.icon_default_header_man);
//                        tvName.setText(medicalHousekeeper.getUsername());
//                        tvServeCount.setText("已服务" + medicalHousekeeper.getServer_number() + "人");
//
//                        int score = medicalHousekeeper.getStar();
//                        if (score <= 0) {
//                            llScore.setVisibility(View.INVISIBLE);
//                        } else {
//                            int dp5 = SizeUtils.dp2px(5);
//                            int starSize = ScreenUtils.getScreenWidth() / 25;
//                            for (int i = 0; i < score; i++) {
//                                ImageView iv = new ImageView(mContext);
//                                iv.setImageResource(R.mipmap.icon_star_full);
//                                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(starSize, starSize);
//                                layoutParams.setMargins(dp5, 0, 0, 0);
//                                llScore.addView(iv, layoutParams);
//                            }
//                        }
//                        ArrayList<String> credentials = medicalHousekeeper.getCredentials();
//                        if (credentials != null && credentials.size() > 0) {
//                            tvCheckQualifications.setOnClickListener(new View.OnClickListener() {
//                                @Override
//                                public void onClick(View v) {
//                                    PhotoBrowse.with(mContext)
//                                            .showType(ShowType.MULTIPLE_URL)
//                                            .url(credentials)
//                                            .show();
//                                }
//                            });
//                        } else {
//                            tvCheckQualifications.setVisibility(View.INVISIBLE);
//                        }
//
//                        ArrayList<Article> articles = medicalHousekeeper.getArticle_list();
//
//                        if (articles != null && articles.size() > 0) {
//                            int size = articles.size();
//                            llArticles.setVisibility(View.VISIBLE);
//                            if (size > 3) {
//                                articles.subList(0, 4);
//                            }
//                            for (int i = 0; i < size; i++) {
//                                LinearLayout linearLayout = new LinearLayout(mContext);
//                                linearLayout.setOrientation(LinearLayout.VERTICAL);
//
//                                Article article = articles.get(i);
//
//                                ImageView iv = new ImageView(mContext);
//                                int width = ScreenUtils.getScreenWidth() / 2;
//                                int height = width / 3 * 2;
//                                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, height);
//                                iv.setLayoutParams(layoutParams);
////                                iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
//                                GlideHelper.setRoundImage(mContext, article.getArticle_image(), iv);
//
//                                linearLayout.addView(iv);
//
//                                TextView tv = new TextView(mContext);
//                                tv.setText(article.getArticle_title());
//                                tv.setTextAppearance(R.style.NormalTextView);
//                                tv.setMaxLines(2);
//                                tv.setEllipsize(TextUtils.TruncateAt.END);
//                                tv.setPadding(0, SizeUtils.sp2px(12), 0, 0);
//                                linearLayout.addView(tv);
//                                linearLayout.setOnClickListener(new View.OnClickListener() {
//                                    @Override
//                                    public void onClick(View v) {
//                                        ArticleDetailActivity.show(mContext, article);
//                                    }
//                                });
//                                if (i != size - 1) {
//                                    linearLayout.setPadding(0, 0, SizeUtils.sp2px(12), 0);
//                                }
//                                llArticlesList.addView(linearLayout);
//                            }
//                            tvCheckAllArticles.setOnClickListener(new View.OnClickListener() {
//                                @Override
//                                public void onClick(View v) {
//                                    ArticleListActivity.show(mContext, medicalHousekeeper.getId());
//                                }
//                            });
//                        }
//
//                        ArrayList<Goods> goodsList = medicalHousekeeper.getGoods_list();
//
//                        if (goodsList != null && goodsList.size() > 0) {
//                            llGoods.setVisibility(View.VISIBLE);
//                            int size = goodsList.size();
//                            recyclerViewGoods.setLayoutManager(new LinearLayoutManager(mContext));
//                            MedicalHousekeeperGoodsAdapter adapter = new MedicalHousekeeperGoodsAdapter(mContext);
//                            recyclerViewGoods.setAdapter(adapter);
//                            if (size > 3) {
//                                adapter.refresh(new ArrayList<>(goodsList.subList(0, 3)));
//                            } else {
//                                adapter.refresh(goodsList);
//                            }
//
//                            tvCheckAllGoods.setOnClickListener(new View.OnClickListener() {
//                                @Override
//                                public void onClick(View v) {
//                                    MedicalHousekeeperGoodsActivity.show(mContext, medicalHousekeeper.getId());
//                                }
//                            });
//                        }
//                    }
//                });

        if (userId != userInfoManager.getLoginUserId()) {
            tvChat.setVisibility(View.VISIBLE);
            tvChat.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    HashMap<String, Object> params = new HashMap<>();
                    params.put("id", userId);
                    params.put("role_id", 4);
                    requestManager.request(requestManager.mMessageRetrofitService.getPrivateChatSetting(RequestManager.getJsonRequestBody(params)),
                            new BaseObserverImpl<ContactUser>(mContext) {
                                @Override
                                public void onSuccess(ContactUser contactUser) {
                                    super.onSuccess(contactUser);

                                    ChatMessageActivity.show(mContext, contactUser);
                                }
                            });
                }
            });
        }
    }

    private void getArticles() {
        HashMap<String, Object> params = new HashMap<>();
        params.put("id", userId);
        params.put("page_index", 1);
        params.put("page_size", 4);
        requestManager.request(requestManager.mJavaRetrofitService.getArticles(RequestManager.getJsonRequestBody(params)),
                new BaseObserverImpl<ArrayList<Article>>() {
                    @Override
                    public void onSuccess(ArrayList<Article> articles) {
                        super.onSuccess(articles);

                        if (articles != null && articles.size() > 0) {
                            int size = articles.size();
                            llArticles.setVisibility(View.VISIBLE);
                            if (size > 3) {
                                articles.subList(0, 3);
                            }
                            for (int i = 0; i < size; i++) {
                                LinearLayout linearLayout = new LinearLayout(mContext);
                                linearLayout.setOrientation(LinearLayout.VERTICAL);

                                Article article = articles.get(i);

                                ImageView iv = new ImageView(mContext);
                                int width = ScreenUtils.getScreenWidth() / 2;
                                int height = width / 3 * 2;
                                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, height);
                                iv.setLayoutParams(layoutParams);
//                                iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
                                GlideHelper.setRoundImage(mContext, article.getArticle_image(), iv);

                                linearLayout.addView(iv);

                                TextView tv = new TextView(mContext);
                                tv.setText(article.getArticle_title());
                                tv.setTextAppearance(R.style.NormalTextView);
                                tv.setMaxLines(2);
                                tv.setEllipsize(TextUtils.TruncateAt.END);
                                tv.setPadding(0, SizeUtils.sp2px(12), 0, 0);
                                linearLayout.addView(tv);
                                linearLayout.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        ArticleDetailActivity.showByArticleId(mContext, article);
                                    }
                                });
                                if (i != size - 1) {
                                    linearLayout.setPadding(0, 0, SizeUtils.sp2px(12), 0);
                                }
                                llArticlesList.addView(linearLayout);
                            }
                            tvCheckAllArticles.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    ArticleListActivity.show(mContext, userId);
                                }
                            });
                        }
                    }
                });
    }

    private void getGoods() {
        HashMap<String, Object> params = new HashMap<>();
        params.put("id", userId);
        params.put("page_index", 1);
        params.put("page_size", 4);

        requestManager.request(requestManager.mJavaRetrofitService.getGoods(RequestManager.getJsonRequestBody(params)),
                new BaseObserverImpl<ArrayList<Goods>>() {
                    @Override
                    public void onSuccess(ArrayList<Goods> goodsList) {
                        super.onSuccess(goodsList);

                        if (goodsList != null && goodsList.size() > 0) {
                            llGoods.setVisibility(View.VISIBLE);
                            int size = goodsList.size();
                            recyclerViewGoods.setLayoutManager(new LinearLayoutManager(mContext));
                            MedicalHousekeeperGoodsAdapter adapter = new MedicalHousekeeperGoodsAdapter(mContext);
                            recyclerViewGoods.setAdapter(adapter);
                            if (size > 3) {
                                adapter.refresh(new ArrayList<>(goodsList.subList(0, 3)));
                            } else {
                                adapter.refresh(goodsList);
                            }

                            tvCheckAllGoods.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    MedicalHousekeeperGoodsActivity.show(mContext, userId);
                                }
                            });
                        }
                    }
                });
    }
}
























